﻿#region Copyright © 2011 Prageeth Silva <prageeth@thenewcoders.com>
/*
 * This software is provided 'as-is', without any express or implied warranty.
 * In no event will the author(s) be held liable for any damages arising from
 * the use of this software.
 * 
 * Permission is granted to anyone to use this software for any purpose,
 * including commercial applications, and to alter it and redistribute it
 * freely, subject to the following restrictions:
 * 
 *   1. The origin of this software must not be misrepresented; you must not
 *      claim that you wrote the original software. If you use this software
 *      in a product, an acknowledgment in the product documentation would be
 *      appreciated but is not required.
 * 
 *   2. Altered source versions must be plainly marked as such, and must not
 *      be misrepresented as being the original software.
 * 
 *   3. This notice may not be removed or altered from any source distribution.
 */
#endregion

using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Text.RegularExpressions;

namespace SourcePlus.Core
{
    internal class DirectoryAnalyser
    {

        public DirectoryAnalyser()
        {

        }

        public string[] GetFileTypes(string path, bool checkSubDirectories, bool ignoreHidden)
        {

            if (!Directory.Exists(path)) { return new string[] { }; }

            string[] files = Directory.GetFiles(path, "*", (checkSubDirectories) ? SearchOption.AllDirectories : SearchOption.TopDirectoryOnly);
            List<string> list = new List<string>(files.Length);
                        
            Regex rgxHidden = (ignoreHidden) ? new Regex(@"^((?!\\\.).)*$") : null;
            
            foreach (string f in files)
            {
                try
                {
                    if (ignoreHidden && !rgxHidden.IsMatch(f)) { continue; }
                    string s = f.Substring(f.LastIndexOf('.')+1).ToLower();
                    if (!list.Contains(s)) { list.Add(s); }
                }
                catch
                {
                    continue;
                }
            }

            return list.ToArray();

        }

        public string[] GetFilesList(string path, string[] filetypes, bool checkSubDirectories, bool ignoreHidden)
        {

            bool anytype = (filetypes == null || filetypes.Length == 0);

            if (!Directory.Exists(path)) { return new string[] { }; }
            
            string[] files = Directory.GetFiles(path, "*", (checkSubDirectories) ? SearchOption.AllDirectories : SearchOption.TopDirectoryOnly);            
            
            // builder a regex filter
            int len = (!anytype) ? 20 + filetypes.Length * 4 : 20;
            StringBuilder sb = (ignoreHidden) ? new StringBuilder(@"^((?!\\\.).)*", len) :
                                                new StringBuilder(len);
           
            if (!anytype)
            {
                sb.Append(@"(?<=.*\.)(");
                foreach (string s in filetypes)
                {
                    sb.Append(s);
                    sb.Append('|');
                }
                sb.Replace('|', ')', sb.Length - 1, 1);
            }
            sb.Append(@"$");

            Regex rgx = new Regex(sb.ToString(), RegexOptions.IgnoreCase);

            List<string> list = new List<string>(files.Length);
            foreach (string f in files)
            {
                if (rgx.IsMatch(f)) { list.Add(f); }
            }
            return list.ToArray();

        }

    }
}
